/*
 * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
 *
 * $Id: SparseNSpace.java 260 2005-05-15 13:08:53Z cgruber $
 */
package net.israfil.foundation.collections.nspace;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * An implementatino of NSpace that is sparsely populated.
 * 
 * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
 * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
 * @version $Revision: 260 $
 */
public class SparseNSpace implements NSpace {

	private final Set<Dimension> dimensions = new HashSet<Dimension>();

	public SparseNSpace() {
		super();	
	}

	public SparseNSpace(Set<Dimension> initialDimensions) {
		this();
		for (Dimension d : dimensions) {
			// This is temporary, until we have an internal protected mutator.
			dimensions.add(d);
		}
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#createDimension(java.lang.String, java.lang.String)
	 */
	public Dimension createDimension(String name, String initialSlot)
		throws DimensionExistsException {
		Dimension d = new SimpleDimension(name,initialSlot);
		dimensions.add(d);
		return d;
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#deleteDimension(org.frugenplat.framework.nspace.Dimension)
	 */
	public void deleteDimension(Dimension dimension) {
		throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#dimensionAddedPosition(org.frugenplat.framework.nspace.Dimension, java.lang.String)
	 */
	public void dimensionAddedPosition(Dimension dimension, String position) {
		throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#dimensionRemovedPosition(org.frugenplat.framework.nspace.Dimension, java.lang.String)
	 */
	public void dimensionRemovedPosition(
		Dimension dimension,
		String position) {
			throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#getDimension(java.lang.String)
	 */
	public Dimension getDimension(String name) {
		throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * Returns an immutable set of dimensions for inspection..
	 * @see org.frugenplat.framework.nspace.NSpace#getDimensions()
	 */
	public Set<Dimension> getDimensions() {
		return Collections.unmodifiableSet(dimensions);
	}
	
	/**
	 * Exposes the internal dimensions set to any children
	 * @return Set
	 */
	protected Set<Dimension> getInternalDimensions() {
		return dimensions;
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#getValue()
	 */
	public Object getValue(Coordinate c) {
		// SparseNSpace s = _slice(c);
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	Object _findValue() {
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	/**
	 * @see org.frugenplat.framework.nspace.NSpace#isPoint()
	 */
	public boolean isPoint() {
		if (dimensions.size() == 0) return true;
		else {
			boolean hasThinDimensions = true;
			for(Dimension d : dimensions){
				hasThinDimensions = hasThinDimensions && 
					(d.size() == 1);
			}
			return hasThinDimensions;				
		}
		
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#lookup(org.frugenplat.framework.nspace.Coordinate)
	 */
	public NSpace lookup(Coordinate c) {
		return _lookup(c);
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#setValue(org.frugenplat.framework.nspace.Coordinate, org.frugenplat.framework.nspace.NSpace)
	 */
	public NSpace setValue(Coordinate c, NSpace value)
		throws InvalidDimensionalityException {
			throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#setValue(org.frugenplat.framework.nspace.Coordinate, java.lang.Object)
	 */
	public NSpace setValue(Coordinate c, Object value)
		throws InvalidDimensionalityException {
			throw new UnsupportedOperationException("Not yet implemented.");
	}

	/**
	 * @see org.frugenplat.framework.nspace.NSpace#slice(org.frugenplat.framework.nspace.Coordinate)
	 */
	public NSpace slice(Coordinate c) {
		return _slice(c);
	}

	private SparseNSpace _lookup(Coordinate c) {
		if (c.equals(Coordinate.EMPTY)) return this;
		throw new UnsupportedOperationException("Not yet implemented.");		
	}

	private SparseNSpace _slice(Coordinate c) {
		if (c.equals(Coordinate.EMPTY)) return this;
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	/* (non-Javadoc)
	 * @see org.frugenplat.framework.nspace.NSpace#slice(org.frugenplat.framework.nspace.Dimension)
	 */
	public NSpace[] slice(Dimension d) {
		// TODO Auto-generated method stub
		return null;
	}
}
